/* ==============================================================================
 * 创 建 者：LYT
 * 创建日期：2018-03-14	
 * 功能描述：

 * 修 改 人:
 * 修改时间:
 * 修改描述：
 * ==============================================================================*/

//购物车商品列表接口
var getBuyCarListUrl = baseUrl + 'MallF/GetBuyCarGoodsList';
//删除商品
var delCarGoodsUrl = baseUrl + 'MallF/RemoveGoods';
//修改商品数量
var editCarGoodsNumberUrl = baseUrl + 'MallF/EditBuyCarNumber';
//提交购物车选中商品
var submitBuyCarSelectedUrl = baseUrl + 'MallF/SubmitSelBuyCarGoods';




var mescroll;
var vu;

$(function() {
	mui.init();
	initVue();
	Vue.nextTick(function(){
		bindEvent();
	})
	
})

function initVue(){
	vu=new Vue({
		el:'#mescroll',
		data:{
			searchVal:'', //搜索内容
			totalPrice : 0,
			checkall : false,
			GoodsList:[],
			chooseGoodInfo:[],
			pageIndex:1,
			PageSize:10,
			totalPage:'',
			memberInfo : '',
			chooseGoodsList : [],
		},
		mounted: function() {
			initMescroll();
//			this.getGoodsList();
		},
		methods:{
			getBuyCarList : getBuyCarList,
			editCarGoodsNumber : editCarGoodsNumber,
			delCarGoods : delCarGoods,
			toBuy : toBuy,
			search:function(){
				if(this.searchVal!=''){
					location.href='searchList.html?goodName='+this.searchVal;
				}else{
					mui.toast('请输入搜索关键词');
				}
			},
			sumPrice : sumPrice,
			checkAllGoods : checkAllGoods,
			submitBuyCarSelected : submitBuyCarSelected,
		},
		computed: {
		    // 计算属性的 getter
		}
	})
}

function submitBuyCarSelected(){
	$(".checkbox").each(function(){
  		if($(this).prop("checked")){
  			var obj = {};
  			obj.GoodsOID = $(this).attr('oid');
  			obj.Count = $(this).parents("li").find(".mui-numbox").attr("num");
  			vu.chooseGoodsList.push(obj);
  		}
  	})
	if(vu.chooseGoodsList.length == 0){
		mui.toast("请选择商品");
		return;
	}
	common.post(submitBuyCarSelectedUrl, {GoodsList : vu.chooseGoodsList},'json',function(r){//JSON.stringify(vu.chooseGoodsList)
		if(r.ErrorCode==0){
			location.href = 'makeSureOrder.html';
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

function checkAllGoods(){
//	vu.chooseGoodsList = [];
	if($('.checkAll').prop("checked")){
	  	vu.checkall = true;
	  	$(".checkbox").each(function(){
	  		$(this).prop("checked",true);
//	  		vu.chooseGoodsList.push($(this).attr("oid"));
	  	})	
	}else{
		$(".checkbox").each(function(){
	  		$(this).prop("checked",false);
	  		vu.totalPrice = 0;
	  		vu.checkall = false;
	  	})
	}
	sumPrice();
}

function sumPrice(){
	vu.totalPrice = 0;
	vu.checkall = true;
	$(".checkbox").each(function(){
  		if($(this).prop("checked")){
  			var n = $(this).parents(".mui-table-view-cell").index();
  			vu.totalPrice += vu.GoodsList[n].GoodsPrice * 100 * vu.GoodsList[n].GoodsNumber / 100;
  			/*var isIn = false;
  			for(var i in vu.chooseGoodsList){
  				if(vu.chooseGoodsList[i] == $(this).attr("oid")){
  					isIn = true;
  				}
  			}
  			if(!isIn){
  				vu.chooseGoodsList.push($(this).attr("oid"));
  			}*/
  		}else{
  			vu.checkall = false;
  			
  		}
  	})
}

function toBuy(){
	
}

function editCarGoodsNumber(oid,n,parentIndex){
	common.get(editCarGoodsNumberUrl, {CarOID : oid, number : n},'json',function(r){
		if(r.ErrorCode==0){
			mui.toast(r.ErrorMsg);
			vu.GoodsList[parentIndex].GoodsNumber = n;
			sumPrice();
			/*vu.pageIndex = 1;
			vu.getBuyCarList();*/
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

function delCarGoods(oid){
	common.post(delCarGoodsUrl, {CarOIDS : oid},'json',function(r){
		if(r.ErrorCode==0){
			mui.toast(r.ErrorMsg);
			vu.pageIndex = 1;
			mescroll.resetUpScroll();
			getBuyCarList();
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

function getBuyCarList(){
	var data = {
		pageIndex : vu.pageIndex,
		pageSize : 10,
	};
	common.get(getBuyCarListUrl, data,'json',function(r){
		if(r.ErrorCode==0){
			vu.totalPage = r.Data.TotalPage;
			mescroll.endByPage(r.Data.TotalItemCount, vu.totalPage);
			if(vu.pageIndex == 1){
				vu.GoodsList = r.Data.List;
			}else{
				vu.GoodsList = vu.GoodsList.concat(r.Data.List);
			}
    		vu.pageIndex++;
    		Vue.nextTick(function(){
    			mui('.mui-numbox').numbox();
				/*$(".mui-numbox").each(function(){
			  		var n = $(this).parents(".mui-table-view-cell").index();
			  		console.log('"#mui-numbox' + n + '"');
			  		mui('"#mui-numbox' + n + '"').numbox().setValue($(this).attr('num'));
			  	})*/
			  	mui('body').on('change','.mui-input-numbox', function(e){
			  		var e =  e || event;
			  		var n = $(e.target).parents('.mui-table-view-cell').index();
					vu.editCarGoodsNumber($(this).attr("oid"),$(this).val(),n);
					
	            });
			})
		}else{
			mui.toast(r.ErrorMsg);
			mescroll.endErr();
		}
	})
}
 
function initMescroll(){
	mescroll = new MeScroll("mescroll", {
		down:{
			use: false, //是否初始化下拉刷新; 默认true
			callback: function(mescroll) {
				vu.pageIndex = 1;
				mescroll.resetUpScroll();
				getBuyCarList();
			}
		},
		up: {
			auto: true,
			isBounce: false, //此处禁止ios回弹,解析(务必认真阅读,特别是最后一点): http://www.mescroll.com/qa.html#q10
			clearEmptyId: "dataList", //1.下拉刷新时会自动先清空此列表,再加入数据; 2.无任何数据时会在此列表自动提示空
			toTop:{ //配置回到顶部按钮
				src : "phoneContent/img/top.png", //默认滚动到1000px显示,可配置offset修改
				//offset : 1000
			},
			htmlNodata: '<p class="upwarp-nodata">-- 没有更多数据了 --</p>', //无数据的布局
			callback: function (){//上拉回调
				getBuyCarList();
			},
			page : {
				size : 10, 
			},
		}
	})
}


function bindEvent(){
	$("nav").on("tap", "a", function() { //点击触发  
		location.href = $(this).attr("href");
	})
}
